package com.study.hisapi.service.hospitalXTService;

import cn.hutool.json.JSONUtil;
import com.study.hisapi.constant.HospitalServiceConstant;
import com.study.hisapi.domain.bo.UserInfoBo;
import com.study.hisapi.domain.vo.UserInfoVo;
import com.study.hisapi.exception.ValidatedException;
import com.study.hisapi.service.AbstractRequestService;
import com.study.hisapi.utils.ValidatedUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.validation.ConstraintViolation;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

/**
 * 信通通用接口
 *
 * @author LiXiao
 * @date 2024/4/12
 */
@Slf4j
@Service(HospitalServiceConstant.HOSPITAL_XT)
public class HospitalXTService extends AbstractRequestService {
    @Override
    public UserInfoVo getUserInfo(LinkedHashMap<String, Object> request) {
        // map转bean
        String jsonStr = JSONUtil.toJsonStr(request);
        UserInfoBo userInfoBo = JSONUtil.toBean(jsonStr, UserInfoBo.class);

        Set<ConstraintViolation<UserInfoBo>> violations = ValidatedUtils.isValid(userInfoBo);
        if (!violations.isEmpty()) {
            List<String> errMsg = new ArrayList<>();
            for (ConstraintViolation<UserInfoBo> violation : violations) {
                errMsg.add(violation.getPropertyPath() + ":" + violation.getMessage());
            }
            throw new ValidatedException(String.join("; ", errMsg));
        }


        UserInfoVo userInfoVo = new UserInfoVo();
        userInfoVo.setName("张三");
        userInfoVo.setSex("男");
        userInfoVo.setAge("20");
        return userInfoVo;
    }
}
